<?php

function calculate_timestamp($date , $time ='00:00:00', $addition  = 0 , $is_minus = false){

	return strtotime(calculate_date($date,$time,$addition,$is_minus));

}


function calculate_date($date , $time ='00:00:00', $addition  = 0 , $is_minus = false){
	$date = trim($date);
	$time = trim($time);
	if(strpos($date,'/')!==false){
		list($d,$m,$y)= explode('/',$date);
		$date = "$y-$m-$d";
	}
	$exp = explode( ':' , $time );
	if(!is_array($exp) || count($exp) == 0){
		$time = '00:00:00';
	}elseif(count($exp) == 1){
		$time = $time.':00:00';
	}elseif(count($exp) == 2){
		$time = $time.':00';
	}

	$timestamp = strtotime($date.' '.$time);
	if($addition){

		if($is_minus){
			$timestamp -=$addition;
		}else{
			$timestamp +=$addition;
		}
	}

	return date('Y-m-d H:i:s' , $timestamp);


}













function add_leaves($emp_code , $leaves_item_it , $leave , $out_key , $in_key , $in_out_item ){



	$it = $leaves_item_it->search_array( 'fn',array($in_key,$out_key) );



	if(null==$it ){
		return null;
	}

	$fn_map = $it->fn_map;
	$it2 = new ArrayIterator2(array(),0,$fn_map);


	$max_period = $it2->get_option_namspace('leaves/' . $leave, 'max_period');
	$wait_for_in = false;
	$output_struct = array();
	$last_index = 0;
	foreach($it as $k => $v){

		if($emp_code != $it->get_item('emp_code')){
			continue;
		}
		//if($emp_code != '0001')continue;

		$index = -1;

		if( true == $wait_for_in && $it->get_item('fn') == $in_key ){
			$wait_for_in = false;
			$it2->add('end_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) , $last_index);
			$it2->add('end_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time')), $last_index);
			$it2->add('trans_type' , globals::LEAVE_TRANSACTION ,$last_index);
			//$it2->addItem('start_date',$it->get_item('date'));
			//$deb_cont = true;
			continue;

		}
		if( false == $wait_for_in && $it->get_item('fn') == $in_key ){
			//var_dump($it->get_item());echo "<br>";
			$minus_time = $max_period;
			$previous_in_index = $it->search( 'fn', $in_key, $k-1 , true );
			if($previous_in_index >= 0){
				$prev_in_item = $it->get_item(null,$previous_in_index);
				$minus_time = $it->get_item('timestamp')-$prev_in_item['timestamp'] > $minus_time ? $minus_time : $it->get_item('timestamp')-$prev_in_item['timestamp']-10 ;
				//var_dump($minus_time);exit;
			}else{
				$end_timestamp = $it->get_item('timestamp');
				$start_date = date('Y-m-d',$end_timestamp);
				$start_time = date('H:i:s',$end_timestamp);

				$minus_time = $end_timestamp + $max_period  > $in_out_item['start_timestamp'] ?  $in_out_item['start_timestamp']  - $end_timestamp : $max_period;
				$minus_time = $end_timestamp - $max_period  < $in_out_item['start_timestamp'] ?  $end_timestamp - $in_out_item['start_timestamp']  : $max_period;
			}
			$start_date_time = calculate_date($it->get_item('date'),$it->get_item('time'),$minus_time,true);
			$start_timestamp = calculate_timestamp($it->get_item('date'),$it->get_item('time'),$minus_time,true);
			$index = $it2->add( 'start_date_time' , $start_date_time );
			$it2->add('start_timestamp' , $start_timestamp ,$index);
			$it2->add('trans_type' , globals::NO_CLOSEING_LEAVE_START ,$index);

			$it2->add('end_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) , $index);
			$it2->add('end_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time'))  , $index);





			$wait_for_in = false;
			continue;
		}
		if( false == $wait_for_in  && $it->get_item('fn') == $out_key ){
			$index = $it2->add('start_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) );
			$it2->add('start_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time'))  , $index);
			$it2->add('trans_type' , globals::LEAVE_TRANSACTION ,$index);


			$wait_for_in = true;
			$last_index  = $it2->last_index();
			continue;
		}
		if( true == $wait_for_in  && $it->get_item('fn') == $out_key ){
			$it2->add('end_date_time' , calculate_date($it->get_item('date'),$it->get_item('time'),1,TRUE) , $last_index);
			$it2->add('end_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time'),1,TRUE), $last_index);
			$it2->add('trans_type' , globals::NO_CLOSEING_LEAVE_END ,$last_index);


			$index = $it2->add('start_date_time' , calculate_date($it->get_item('date'),$it->get_item('time')) );
			$it2->add('start_timestamp' , calculate_timestamp($it->get_item('date'),$it->get_item('time')), $index);



			$wait_for_in = true;
			continue;
		}


	}
	foreach($it2 as $k =>$v) {
		//var_dump($it2->get_item('start_timestamp'));
		if($it2->get_item('start_timestamp') === null && $it2->get_item('end_timestamp')===null)continue;
		if( $it2->get_item('start_timestamp') === null ){

			$end_timestamp = $it2->get_item('end_timestamp');
			$start_date = date('Y-m-d',$end_timestamp);
			$start_time = date('H:i:s',$end_timestamp);

			$minus_time = $end_timestamp + $max_period  > $in_out_item['start_timestamp'] ?  $in_out_item['start_timestamp']  - $end_timestamp : $max_period;
			$minus_time = $end_timestamp - $max_period  < $in_out_item['start_timestamp'] ?  $end_timestamp - $in_out_item['start_timestamp']  : $max_period;

			$datetime = calculate_date($start_date, $start_time, $minus_time , true );
			$timestamp = calculate_timestamp($start_date, $start_time, $minus_time , true);
			$it2->add('start_date_time' , $datetime , $k);
			$it2->add('start_timestamp' , $timestamp , $k);
			$it2->add('trans_type' , globals::NO_CLOSEING_LEAVE_START , $k );
		}

		//var_dump($it2->get_item());echo "<br>";
		if( $it2->get_item('end_timestamp') === null /*&& $emp_code !='0005'*/){


			$start_timestamp = $it2->get_item('start_timestamp');
			$end_date = date('Y-m-d',$start_timestamp);
			$end_time = date('H:i:s',$start_timestamp);



			$plus_time = $start_timestamp + $max_period  > $in_out_item['end_timestamp'] ?  $in_out_item['end_timestamp']  - $start_timestamp : $max_period;

			$datetime = calculate_date($end_date, $end_time, $plus_time );
			$timestamp = calculate_timestamp($end_date, $end_time, $plus_time );
			$it2->add('end_date_time' , $datetime , $k);
			$it2->add('end_timestamp' , $timestamp , $k);
			$it2->add('trans_type' , globals::NO_CLOSEING_LEAVE_END ,$k);
		}

	}
	return $it2;

}
